/*
 * @(#)GlobalReferenceDAObject.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.dao;

import java.sql.Connection;
import java.util.Hashtable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
  GlobalReferenceDAObject.java

  This class is a Global Reference Data Access Object.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   25/06/2003
    @version         $Revision: 1.10 $
*/

public class GlobalReferenceDAObject {

  public static final String REVISION = "$Revision: 1.10 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());
  protected Connection dbConn = null;

  public GlobalReferenceDAObject(Connection conn) {
    dbConn = conn;
  }

  /**
   * getAllSysParameter
   *
   * Store all system parameter in a hashtable and return it. The key of
   * Hashtable is the parameter code while the associated object is the
   * parameter value.
   *
   * @return Hashtable containing all system parameters.
   * @throws Exception
   */
  public Hashtable getAllSysParameter() throws Exception {
    java.sql.PreparedStatement preStat = null;
    java.sql.ResultSet rs = null;
    java.util.Hashtable sysParaHash = new java.util.Hashtable();

    try {
      preStat = dbConn.prepareStatement("SELECT PARAMETER_CODE, PARAMETER_VALUE FROM SYS_PARAMETER");
      rs = preStat.executeQuery();
      while (rs.next()) {
        if (rs.getString("PARAMETER_VALUE") != null) {
          sysParaHash.put(rs.getString("PARAMETER_CODE"), rs.getString("PARAMETER_VALUE"));
        } else {
          sysParaHash.put(rs.getString("PARAMETER_CODE"), "");
        }
      }
      return(sysParaHash);
    } catch (Exception e) {
      log.error("Error Getting System Parameter.", e);
      throw e;
    } finally {
      try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
      try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
    }
  }

  /**
   * getAllUserInfo
   *
   * Store all user information in a hashtable and return it. The key of
   * Hashtable is the user record ID while the associated object is a string
   * array. The 1st element of that array is storing the user full name while
   * the 2nd element is storing the user email address.
   *
   * @return
   * @throws Exception
   */
  public Hashtable getAllUserInfo() throws Exception {
    java.sql.PreparedStatement preStat = null;
    java.sql.ResultSet rs = null;
    java.util.Hashtable userInfoHash = new java.util.Hashtable();

    try {
      preStat = dbConn.prepareStatement("SELECT ID, FULL_NAME, EMAIL FROM USER_RECORD");
      rs = preStat.executeQuery();
      while (rs.next()) {
        if (rs.getString("ID") != null) {
          String[] tmpAry = { rs.getString("FULL_NAME"), rs.getString("EMAIL") };
          userInfoHash.put(new Integer(rs.getInt("ID")), tmpAry);
        }
      }
      return(userInfoHash);
    } catch (Exception e) {
      log.error("Error Getting User Info.", e);
      throw e;
    } finally {
      try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
      try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
    }
  }

}
